package cn.itheima.service.impl;

import cn.itheima.entity.PageQueryBean;
import cn.itheima.mapper.UserMapper;
import cn.itheima.pojo.User;
import cn.itheima.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    //   Mybatis插件分页
    @Override
    public Map pageQuery(PageQueryBean pageQueryBean) {
     //  分页参数 传递 PageHelper
        PageHelper.startPage(pageQueryBean.getPageNum(),pageQueryBean.getPageSize());
        //  select *from  tb_user   limit  ?,?  ---LIst<User>
        List<User> userlist = userMapper.findAll(pageQueryBean.getQueryString());
        //  分页查询的结果集 传递给PageInfo对象 自动完成封装分页结果集
        PageInfo<User> pageInfo = new PageInfo<User>(userlist);
        Map map = new HashMap();
        map.put("userlist",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public void deleteUserById(Integer id) {

    }

//    原始分页
//    @Override
//    public Map pageQuery(Integer pageNum, Integer pageSize) {
//        List<User> userlist  =  userMapper.pageQuery((pageNum-1)*pageSize,pageSize);
//        // select * from  xx   where   xxx= xxx  limit  ?,?
//        long  total  = userMapper.findTotalCounts();
//        //  select count(1) from  xxx  where  xx=  xxx
//        Map map = new HashMap();
//        map.put("userlist",userlist);
//        map.put("total",total);
//        return map;
//    }
}
